import turtle
import math
import time


# 初始化画布
screen = turtle.Screen()
screen.bgcolor("black")
screen.tracer(0)  # 关闭自动刷新提升性能
n1=4
n2=3
# 创建18只海龟（3组×6只）
colors = ["red", "orange", "yellow", "green", "blue", "purple",
          "cyan", "pink", "white", "lime", "gold", "violet",
          "brown", "grey", "navy", "maroon", "teal", "silver"]

turtles = []
for i in range(n1*n2):
    t = turtle.Turtle()
    t.shape("turtle")
    t.color(colors[i])
    # t.penup()
    t.speed(0)
    turtles.append(t)
screen.update()

# 第一阶段：3组同心圆辐射（间隔120度）
def phase_1():
    group_angles = [0, 120, 240]  # 3组基准角度

    for step in range(300):
        for i in range(n1*n2):
            group_id = i // n2  # 0-（n1-1）组
            turtle_id = i % n2  # 组内编号0-(n2-1)

            turtles[i].seth(group_id*360/n1)
            turtles[i].fd(1)

        screen.update()
        time.sleep(0.01)


# 第二阶段：每组内6只同心圆辐射
def phase_2():
    for step in range(50):
        for i in range(n1*n2):
            group_id = i // n2
            turtle_id = i % n2

            turtles[i].seth(turtle_id*360/n2)
            turtles[i].fd(1)

        screen.update()
        time.sleep(0.01)


def phase_3():
    z=0
    while z<360:
        z+=1
        for i in turtles:
            i.left(10)
        screen.update()
        time.sleep(0.01)
    screen.tracer(1)
    for i in turtles:
        i.seth(0)
        i.down()
        i.speed(1)
        i.goto(0,0)
        # screen.update()


# time.sleep(1)
# 执行动画
phase_1()
time.sleep(1)
phase_2()
time.sleep(1)
phase_3()
# 保持窗口
turtle.done()
